﻿using CuaHangLego.DataAccess;
using CuaHangLego.Model;
using CuaHangLego.Webform.App_Code;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CuaHangLego.Webform
{
    public partial class Target : BasePage
    {
        SanPham sp;
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request.QueryString.Get("ID"));
            sp = SanPhamDA.Instance.GetByID(id);
            FormView1.DataSource = new object[]{sp};
            FormView1.DataBind();
            /*NameValueCollection nvc = Request.Form;
            if (!string.IsNullOrEmpty(nvc["ID"]))
            {
                Label1.Text = nvc["ID"];
            }*/
            IList<int> l = Session["recentItems"] == null ? new List<int>() : Session["recentItems"] as IList<int>;
            l.Add(id);
            l = l.Distinct().ToList();
            if (l.Count > 5) l.RemoveAt(0);
            Session["recentItems"] = l;
        }

        protected void SameProducts_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                string ID = ((HiddenField)e.Item.FindControl("ProductID")).Value;
                ((HyperLink)e.Item.FindControl("ProductLink")).NavigateUrl = "ProductDetail.aspx?ID=" + ID;

            }
        }

        protected void Button1_Command(object sender, CommandEventArgs e)
        {
            IList<int> li = Session["cart"] as List<int>;
            if (li == null)
                li = new List<int>();
            li.Add(Convert.ToInt32(Request.QueryString.Get("ID")));
            li = li.Distinct().ToList();
            Session["cart"] = li;
            Response.Redirect("~/ShoppingCart.aspx");
        }

    }
}